package io.gitee.hefren.hhconfig.client.conf;

import io.gitee.hefren.hhconfig.client.beans.ConfigsMeta;
import io.gitee.hefren.hhconfig.client.service.HhConfigService;
import io.gitee.hefren.hhconfig.client.service.impl.HhConfigServiceImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Date 2024/5/29
 * @Author lifei
 */
public class HhPropertySourceProcessor implements BeanFactoryPostProcessor, PriorityOrdered, EnvironmentAware, ApplicationContextAware {

    private static final String HH_PROPERTY_SOURCES = "hhPropertySources";
    private static final String HH_PROPERTY_SOURCE = "hhPropertySource";

    private Environment environment;
    private ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;
        MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
        if (propertySources.contains(HH_PROPERTY_SOURCES)) {
            System.out.println("===> already addFirst: " + HH_PROPERTY_SOURCES);
            return;
        }
        String capp = environment.getProperty("hhconfig.app", "app01");
        String cenv = environment.getProperty("hhconfig.env", "dev");
        String cnamespace = environment.getProperty("hhconfig.namespace", "application");
        String configServer = environment.getProperty("hhconfig.configServer", "http://localhost:9192");
        ConfigsMeta configsMeta = new ConfigsMeta(capp, cenv, cnamespace, configServer);
        HhConfigService hhConfigService = HhConfigService.getDefault(applicationContext, configsMeta);
        PropertySource<HhConfigService> hhPropertySource = new HhPropertySource(HH_PROPERTY_SOURCE, hhConfigService);
        // 可以组合多个 PropertySource
        CompositePropertySource compositePropertySource = new CompositePropertySource(HH_PROPERTY_SOURCES);
        compositePropertySource.addPropertySource(hhPropertySource);
        // 放入第一个位置
        propertySources.addFirst(compositePropertySource);
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
